﻿using System;
using System.Linq;
using System.Web.Mvc;
using MiddleMvcTask.Data;
using MiddleMvcTask.Data.DataModels;
using MiddleMVCTask.BL;

namespace MiddleMvcTask.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        /// <summary>
        /// Ajax action for submit some data
        /// </summary>
        /// <param name="age">age entered by user</param>
        /// <returns></returns>
        public ActionResult SubmitData(string age)
        {
            if (Request.IsAjaxRequest())
            {
                try
                {
                    var ageAsInt = Int32.Parse(age);

                    if (ageAsInt > 32)
                    {
                        return Json(new { text = "You're bigger then 32" }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        return Json(new { text = "You're not bigger then 32" }, JsonRequestBehavior.AllowGet);
                    }
                }
                catch (Exception) // it is not a good practise
                {
                    return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Invalid Number");
                }
            }

            return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, "Invalid Request");
        }

        /// <summary>
        /// Redirect to Error View 
        /// </summary>
        /// <returns>View</returns>
        public ActionResult Error()
        {
            return View();
        }
        
        /// <summary>
        /// Show home page when user login 
        /// </summary>
        /// <returns>View</returns>
        public ActionResult Index()
        {
            return View();
        }
    }
}